/**
 * 
 */
package com.auth.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.auth.config.Config;
import com.auth.model.Account;
import com.auth.model.CpAccount;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;
import com.utils.encrypt.MD5;

public class CpAccountServiceImpl extends AbstractService implements CpAccountService
{
	private Logger logger;
	
	private final String ENCRYPT_KEY = "@$^adpopo^$@";
	
	public CpAccountServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	
	
	public String encrypt(String str)
	{
		return MD5.computeMD5(str + ENCRYPT_KEY);
	}
	
	@Override
	public CpAccount getCpAccount(String id, String password) 
	{		
		CpAccount a = getCpAccount(id);
		if (a != null && (a.getPassword().equals(password) || a.getPassword().equals(encrypt(password))))
			return a;
		return null;
	}

	@Override
	public CpAccount getCpAccount(String id) 
	{
		String sql = "select * from cpaccount where id='" + id + "'";
		ModelGenerator<CpAccount> dbcpmg = new ModelGenerator<CpAccount>(CpAccount.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<CpAccount> getCpAccounts()
	{
		String sql = "select * from cpaccount";
		logger.debug("sql: " + sql);
		ModelGenerator<CpAccount> dbcpmg = new ModelGenerator<CpAccount>(CpAccount.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveCpAccount(Account operator,String type, CpAccount account)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
				sql = 
				"insert into cpaccount(id,name,password,cpid,channelid,type,remark) " +
				"values('" + encodeDb(account.getId()) + "','" + encodeDb(account.getName()) + "','" + encrypt(account.getPassword()) + "','" +
						account.getCpId()+ "','" +
						account.getChannelid()+ "','" +
						account.getType()+ "','" + account.getRemark() + "')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update cpaccount set name='" + encodeDb(account.getName()) + "'," +
						"remark='" + account.getRemark() + "',cpid='"+account.getCpId()+"',channelid='"+account.getChannelid()+"'  " + 
				" where id='" + account.getId() + "'";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from cpaccount where id='" + encodeDb(account.getId()) + "'";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				CpAccount lo = null;
				if (!type.equals("delete"))
					lo = getCpAccount(account.getId());
				this.logTableOperate("cpaccount", type, operator, lo);
			}
		}		
		return false;
	}
	public boolean updatePassword(Account operator, String id, String password)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		String sql =
				"update cpaccount set password='" + encrypt(password) + "' " +
				"where id='" + id + "'";
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				CpAccount lo = getCpAccount(id);
				this.logTableOperate("cpaccount", "update", operator, lo);
				return true;
			}
		}		
		return false;
	}

}
